package com.github.pig.admin.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.sys.SysRoleDeptMapper;
import com.github.pig.admin.mapper.sys.SysRoleMapper;
import com.github.pig.admin.model.dto.RoleDTO;
import com.github.pig.admin.model.entity.RoleBean;
import com.github.pig.admin.model.entity.SysDept;
import com.github.pig.admin.model.entity.SysRole;
import com.github.pig.admin.model.zhgl.building.CommunityBuildingTree;
import com.github.pig.admin.service.sys.SysDeptService;
import com.github.pig.admin.service.sys.SysRoleService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2017-10-29
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysDeptService deptService;

    /**
     * 添加角色
     *
     * @param roleDto 角色信息
     * @return 成功、失败
     */
    @Override
    public Boolean insertRole(RoleDTO roleDto) {
        SysRole sysRole = new SysRole();
        //判断角色标识是否重复
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("role_code",roleDto.getRoleCode());
        List<SysRole> sysRoleList = this.selectList(entityWrapper);
        if(sysRoleList!=null&&sysRoleList.size()>0){
            return false;
        }
        BeanUtils.copyProperties(roleDto, sysRole);
        sysRoleMapper.insert(sysRole);
//        SysRoleDept roleDept = new SysRoleDept();
//        roleDept.setRoleId(sysRole.getRoleId());
//        roleDept.setDeptId(roleDto.getRoleDeptId());
//        sysRoleDeptMapper.insert(roleDept);
        return true;
    }

    /**
     * 分页查角色列表
     *
     * @param query   查询条件
     * @param wrapper wapper
     * @return page
     */
    @Override
    public Page selectwithDeptPage(Query<Object> query, EntityWrapper<Object> wrapper) {
        query.setRecords(sysRoleMapper.selectRolePage(query, query.getCondition()));
        return query;
    }

    /**
     * 更新角色
     *
     * @param roleDto 含有部门信息
     * @return 成功、失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateRoleById(RoleDTO roleDto) {
        //删除原有的角色部门关系
//        SysRoleDept condition = new SysRoleDept();
//        condition.setRoleId(roleDto.getRoleId());
//        sysRoleDeptMapper.delete(new EntityWrapper<>(condition));
//        //判断角色标识是否重复
//        EntityWrapper entityWrapper = new EntityWrapper();
//        entityWrapper.eq("role_code",roleDto.getRoleCode());
//        List<SysRole> sysRoleList = this.selectList(entityWrapper);
//        if(sysRoleList!=null&&sysRoleList.size()>0){
//            return false;
//        }
        //更新角色信息
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDto, sysRole);
        sysRoleMapper.updateById(sysRole);

        //维护角色部门关系
//        SysRoleDept roleDept = new SysRoleDept();
//        roleDept.setRoleId(sysRole.getRoleId());
//        roleDept.setDeptId(roleDto.getRoleDeptId());
//        sysRoleDeptMapper.insert(roleDept);
        return true;
    }

    /**
     * 通过部门ID查询角色列表
     *
     * @param deptId 部门ID
     * @return 角色列表
     */
    @Override
    public List<SysRole> selectListByDeptId(Integer deptId) {
        return sysRoleMapper.selectListByDeptId(deptId);
    }

    /**
     * 通过当前登录用户所属部门获取到子部门下所有角色
     *
     * @param deptId 部门ID
     * @return 角色列表
     */
    @Override
    public Page<SysRole> selectListByCurrentUser(Page<SysRole> page,Integer deptId) {
        List<SysRole> rolePage = sysRoleMapper.selectListByCurrentUser(page, deptId);
        page.setRecords(rolePage);
        return page ;
    }

    @Override
    public Page<SysRole> selectListByCurrentUser(Page<SysRole> page,RoleBean bean) {
        List<SysRole> rolePage = sysRoleMapper.selectListByCurrentUser(page, bean);
        page.setRecords(rolePage);
        return page ;
    }

    @Override
    public SysRole selectByCode(String code) {
        return sysRoleMapper.selectByCode(code);
    }

    @Override
    public List<SysRole> selectListByCurrentUser(RoleBean bean) {
        return sysRoleMapper.selectListByCurrentUser(bean);
    }

    @Override
    public List<CommunityBuildingTree> getDeptRoleByParent(EntityWrapper entityWrapper, SysDept parent) {
        SysDept company = deptService.selectById(parent.getParentId());
        List<SysDept> depts = deptService.selectList(entityWrapper);
        List<CommunityBuildingTree> list = new ArrayList<>();
        for(SysDept dept : depts){
            CommunityBuildingTree node = new CommunityBuildingTree();
            node.setId(dept.getDeptId()==null?"":dept.getDeptId().toString());
            node.setParentId(dept.getParentId()==null?"":dept.getParentId().toString());
            node.setName(dept.getName());
            node.setCompanyId(dept.getCompanyId());
            node.setLeaf(false);
            list.add(node);
        }
        if(!CommonConstant.DEPTPROPERTY_PARK.equals(parent.getDeptProperty())){
            EntityWrapper roleWrapper = new EntityWrapper<SysRole>();
            roleWrapper.eq("community_id",parent.getParentId());
            List<SysRole> roles = this.selectList(roleWrapper);
            for(SysRole role : roles){
                CommunityBuildingTree node = new CommunityBuildingTree();
                node.setId(role.getRoleId()==null?"":role.getRoleId().toString());
                node.setParentId(parent.getParentId()==null?"":parent.getParentId().toString());
                node.setName(role.getRoleName());
                node.setCompanyId(company==null?0:company.getCompanyId());
                node.setLeaf(true);
                list.add(node);
            }
        }
        return list;
    }
}
